/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operators;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author marcin
 */
public class AddOperator extends OperatorImpl {
    

    @Override
    public Double calculateHappiness(Map<String,Double> map) {
                System.out.println("Calculating happiness with additive operator...");
        Double H = 1.0;
        Double minFromEssential=1.0;
        Double satisfaction=0.0;
        //firsly, choose the lowest from essential
        for(String s : map.keySet()) {
            if(this.essenMap.get(s).booleanValue()==true) {
                Double currentVal = map.get(s);
                if(currentVal<minFromEssential) {
                    minFromEssential=currentVal;
                }
            }
        }
        //secondly, sum all importance weights
        Double sumOfWeights=0.0;
        for(Double d : this.impMap.values()) {
            sumOfWeights+=d;
        }
        //thridly, compute satisfaction as weighted sum
        for(String s : map.keySet()) {
            satisfaction+=(this.impMap.get(s)/sumOfWeights)*map.get(s);
        }
        //finally, choose lower from minEssentia and satisfaction
        if(minFromEssential<=satisfaction) {
            H=minFromEssential;
        } else {
            H=satisfaction;
        }   
        System.out.println("Calculated happiness from additive : " + H*this.hMax);
        return H*this.hMax;
    }

    @Override
    public void showDialog(JFrame frame, final List<String> inputs) {

        final ArrayList<JTextField> tempFields = new ArrayList<JTextField>();
        final JDialog dialog = new JDialog(frame,true);
        dialog.setLocationRelativeTo(frame);
        dialog.setSize(new Dimension(200,(100*inputs.size())));
        dialog.setLayout(new BorderLayout());
        
        JPanel j = new JPanel();
        j.setLayout(new GridLayout((inputs.size()*3)+1, 2));
        JPanel northPanel = new JPanel(new BorderLayout());
        JLabel title = new JLabel("Please fill the parametrs for operator: ");
        title.setHorizontalAlignment(JLabel.CENTER);
        northPanel.add(title,BorderLayout.CENTER);
        
        JLabel h = new JLabel("Hmax");
        h.setHorizontalAlignment(JLabel.CENTER);
        JTextField field0 = new JTextField();
        field0.setText(this.hMax.toString());
        j.add(h);
        j.add(field0);
        tempFields.add(field0);
        
        for(String s : inputs) {
            
         j.add(new JLabel("Goal name: "));
         JLabel label0 = new JLabel(s);
         label0.setHorizontalAlignment(JLabel.CENTER);
         j.add(label0);
         
        JLabel label1 = new JLabel("IMPORTANCE");
        label1.setHorizontalAlignment(JLabel.CENTER);
        JTextField field1 = new JTextField("LOW/MEDIUM/HIGH");
        j.add(label1);
        j.add(field1);
        tempFields.add(field1);

        JLabel label2 = new JLabel("ESSENTIALITY");
        label2.setHorizontalAlignment(JLabel.CENTER);
        JTextField field2 = new JTextField("YES/NO");
        j.add(label2);
        j.add(field2);
        tempFields.add(field2);
        }

        dialog.add(j, BorderLayout.CENTER);
        JButton ok = new JButton(" OK");
        ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int filedCounter = 0;
                AddOperator.this.hMax = Double.valueOf(tempFields.get(filedCounter++).getText());
                for (String s : inputs) {
                    //parsing importance
                    String importane = tempFields.get(filedCounter++).getText();
                    if (importane.equals("MEDIUM")) {
                        AddOperator.this.impMap.put(s, AddOperator.IM_MED);
                    } else if (importane.equals("HIGH")) {
                        AddOperator.this.impMap.put(s, AddOperator.IM_HIG);
                    } else if (importane.equals("LOW")) {
                        AddOperator.this.impMap.put(s, AddOperator.IM_LOW);
                    }else {
                        //throw some exception
                    }
                    //parsing essentiality
                    String essenitial = tempFields.get(filedCounter++).getText();
                    if (essenitial.equals("YES")) {
                        AddOperator.this.essenMap.put(s, true);
                    } else if (essenitial.equals("NO")) {
                        AddOperator.this.essenMap.put(s, false);
                    } else {
                        //throw new NumberFormatException();
                    }
                }
                dialog.dispose();
            }
        });
        dialog.add(northPanel, BorderLayout.NORTH);
        dialog.add(ok,BorderLayout.SOUTH);             
        dialog.setVisible(true);
    }
}
